package com.cjh.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjh.common.entity.Role;
import com.cjh.common.entity.RolePermission;
import com.cjh.common.entity.UserRole;
import com.cjh.common.mapper.RolePermissionMapper;
import com.cjh.common.mapper.UserRoleMapper;
import com.cjh.common.response.Result;
import com.cjh.common.service.RolePermissionService;
import com.cjh.common.service.RoleService;
import com.cjh.common.mapper.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 *
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role>
    implements RoleService{
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    RolePermissionMapper rolePermissionMapper;
    @Autowired
    UserRoleMapper userRoleMapper;
    @Override
    public Set<String> getAllRoleNamesByUsername(String username) {
        return roleMapper.selectAllRolesByUsername(username);
    }
    @Override
    public Result getAllRole() {
        List<Role> roles = roleMapper.selectList(null);
        return new Result().setCode(200).setMessage("操作成功！").setData(roles);
    }

    @Override
    public Result getRoleByRoleId(Integer id) {
        Set<Integer> roleByRoleId = roleMapper.getRoleByRoleId(id);
        return new Result().setCode(200).setMessage("操作成功！").setData(roleByRoleId);
    }

    @Override
    public Result updateRoleByRoleId(Integer id, HashSet list) {
        Set<Integer> roleByRoleId = roleMapper.getRoleByRoleId(id);
        Set<Integer> add = new HashSet<>();
        add.addAll(list);
        add.removeAll(roleByRoleId);
        Set<Integer> reduce = new HashSet<>();
        reduce.addAll(roleByRoleId);
        reduce.removeAll(list);
        RolePermission rolePermission = new RolePermission();
        rolePermission.setRoleId(id);
        for (Integer i :add){
            rolePermission.setPermissionId(i);
            rolePermissionMapper.insert(rolePermission);
        }
        for (Integer i :reduce){
            rolePermissionMapper.delete(new QueryWrapper<RolePermission>().eq("permission_id",i));
        }
        return new Result().setCode(200).setMessage("修改成功！");
    }

    @Override
    public Result updateRole(Role role) {
        return new Result().setCode(200).setMessage("修改成功！").setData(roleMapper.updateById(role));
    }

    @Override
    public Result addRole(Role role) {
        return new Result().setCode(200).setMessage("增加成功！").setData(roleMapper.insert(role));
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public Result deleteRoleById(Integer id) {
        if(id == 1 || id == 2 || id == 3){
            return new Result().setCode(201).setMessage("超级管理员角、门店管理员、员工角色无法删除！");
        }
        rolePermissionMapper.delete(Wrappers.<RolePermission>lambdaQuery().eq(RolePermission::getRoleId,id));
        userRoleMapper.delete(Wrappers.<UserRole>lambdaQuery().eq(UserRole::getRoleId,id));
        roleMapper.deleteById(id);
        return new Result().setCode(200).setMessage("删除成功！");
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public Result deleteRoleByIdList(List<Integer> list) {
        ArrayList<Object> objects1 = new ArrayList<>(Arrays.asList(1,2,3));
        boolean b = list.removeAll(objects1);
        Result<Object> objectResult = new Result<>();
        if(b){
            objectResult.setCode(201).setMessage("超级管理员角、门店管理员、员工角色无法删除,其他角色删除成功！");
        }else {
            objectResult.setCode(200).setMessage("删除成功！");
        }
        for (Integer id : list){
            rolePermissionMapper.delete(Wrappers.<RolePermission>lambdaQuery().eq(RolePermission::getRoleId,id));
            userRoleMapper.delete(Wrappers.<UserRole>lambdaQuery().eq(UserRole::getRoleId,id));
            roleMapper.deleteById(id);
        }
        return objectResult;
    }
}




